<?php

namespace website\controllers;

use Yii;
use common\helpers\Util;
use common\helpers\Checker;
use common\helpers\Captcha;
use common\server\sms\SmsCaptcha;
use common\models\User;
use common\models\Category;
use common\models\Design;
use common\models\UserCoupon;

class WelcomeController extends Controller {

    /**
     * Displays homepage.
     * 网站首页
     * @return string
     */
    public function actionIndex()
    {
        $recommendDesigner = User::search()->orderBy('registor DESC')->limit(21)->all();
        $recommendDesign = Design::find()->with('designer')->where(['deleted_at' => 0])
            ->orderBy(['collection' => 'desc', 'view' => 'desc'])
            ->limit(9)->all();
        return $this->render('index', [
            'modelSelector' => Category::selector($this->params['designModelCategories']),
            'recommendDesigner' => array_slice($recommendDesigner, 0, 18),
            'newlyDesigner' => array_slice($recommendDesigner, 1, 4),
            'recommendDesign' => $recommendDesign,
        ]);
    }
    public function actionTest()
    {
        $this->v(UserCoupon::creator(UserCoupon::RegisterDeliver, $this->user->id));
        
        return $this->v($this->user->coupons);
        
        $mobiles = [
            '18358111894', '18605636449', '13185089839', '18816890186', '18042300325',
            '18358111894', '18605636449', '13185089839', '18816890186', '18042300325'
        ];
        foreach($mobiles as $mobile) {
            $response = SmsCaptcha::captcha($mobile, 'register');
            $this->v($response);
        }
        // $server = new RechargeServer(Recharge::PlatformAlipay);
        // $response = $server->recharge(['order_number' => '123', 'amount' => 1, 'remark' => '测试支付']);
        // $this->v($response);
    }

    /**
     * captcha
     */
    public function actionCaptcha()
    {
        $rule = [
            'param' => [
                'name' => ['名称', ['username', 'required']],
            ]
        ];
        $params = $this->request->get();
        $checker = Checker::authentication($rule, $params);
        // 参数异常，渲染错误页面
        if($checker['code'] != Checker::SuccessCode) {
            return 'Error';
        }
        // 生成图形验证码
        Captcha::send($params['name']);
    }
    
    /**
     * 发送手机验证码
     * @return mixed
     */
    public function actionCaptchaMobile()
    {
        $rule = [
            'param' => [
                'mobile' => ['手机号', ['mobile', 'required']],
            ],
        ];
        $params = $this->request->post();
        $checker = Checker::authentication($rule, $params);
        if($checker['code'] != Checker::SuccessCode) {
            return $this->json('Invalid.Param', $checker['message'].'（Invalid Param）');
        }
        // 加锁
        $lockKey = 'register.'.$params['mobile'];
        if( ! Util::lock($lockKey)) {
            return $this->json('Send.Failed', '请不要频繁的操作，稍后再试！');
        }
        if(SmsCaptcha::captcha($params['mobile'], 'register')) {
            return $this->json(SuccessCode, '短信验证码已发送，请注意查收！');
        }
        // 解锁
        Util::unLock($lockKey);
        return $this->json('Send.Failed', '短信验证码发送失败，请重试！');
    }
}
